/**
 * Created by LiuJie on 2017/10/16.
 */
var express = require("express");
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var util = require('../../util/commonutil');
var auth = require('../../auth/auth');
var editRoleRouter = express.Router();
var resultJSON = require('../common/resultJSON');
editRoleRouter.get("/",function (req, res, next) {
    res.render("dataManage/addRoleData",{id:req.param("id"),titleTxt:req.param("id") ? "修改" : "新增"});
});
editRoleRouter.post('/querySysPrivilege', function (req, res, next) {

    var pathname = url.parse(req.url).pathname;  //pathname => list

    // var arg = url.parse(req.url).query;          //arg => name=a&id=5
    // console.log("Request for " + arg );
    // var str = qstr.parse(arg);                //str=> {name:'a',id:'5'}

    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    // console.log("Request for " + arg1 );

    // var name = qstr.parse(arg).draw;         //name => a
    // console.log("name = "+name);
    var query = req.query;//参数
    var sid = auth.getSessionID(req, res, next);
    var qs = req.body;
    arg.sid = sid;
    var data = [
        {id:1, parentId:0, name: "父节点1"},
        {id:11, parentId:1, name: "子节点1"},
        {id:12, parentId:1, name: "子节点2"}
    ];
    //res.json(data);
    //req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr + '/employeeRole/querySysPrivilegeAsZTree',
        method: "GET",
        qs: arg
    }, function (error, response, body) {
        console.log("查询权限数据>>"+body);
        if (!error && response.statusCode == 200) {
            var data = JSON.parse(body);
            if (data) {
                res.json(data);
            } else {
                res.json("");
            }
        }
    });
});
editRoleRouter.post("/addRole",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/employeeRole/save",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("增加角色设备数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            resultJSON.isSuccess = true;
            resultJSON.data = result[0];
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
editRoleRouter.post("/addRolePrivilege",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/employeeRole/saveRolePrivilege",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("增加角色设备数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == "true"){
                resultJSON.isSuccess = true;
                //resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="保存数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
editRoleRouter.post("/queryRoleInfo",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/employeeRole/edit",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("查询角色数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            resultJSON.isSuccess = true;
            resultJSON.data = result;
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="查询数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});

editRoleRouter.post("/updateRole",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/employeeRole/update",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("修改角色权限数据：>>"+body);
        var resJSON = {
            message:"",
            isSuccess:true,
            data:null
        };
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            resJSON.isSuccess = true;
            resJSON.data = result[0];
        }else{
            resJSON.isSuccess=false;
            resJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resJSON);
    });
});
module.exports = editRoleRouter;